package com.bms.controller;

import com.bms.constant.Constants;
import com.bms.enums.ResultStatus;
import com.bms.model.*;
import com.bms.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    //添加图书
    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo){
        //校验参数
        log.info("添加图书,接收到参数bookInfo:{}",bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || bookInfo.getStatus() == null){
            return "输入参数不合法";
        }
        //添加图书
        try {
            Integer result = bookService.insertBook(bookInfo);
            if (result > 0){
                return "";
            }
        }catch (Exception e){
            log.error("添加图书异常, e:", e);
        }
        return "添加失败";
    }

    //查询图书列表
    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session){
        log.info("查询图书列表, 请求参数pageRequest: {}", pageRequest);
//        UserInfo loginUserInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
//        if (loginUserInfo == null || loginUserInfo.getId() <= 0){
//            return Result.nologin();
//        }
        //可以添加参数校验
        PageResult<BookInfo> bookList = bookService.getBookListByPage(pageRequest);
        return Result.success(bookList);
    }

    //查询图书信息
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("根据ID查询图书信息, id:" + bookId);
        return bookService.queryBookById(bookId);
    }

    //更新图书
    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书, bookInfo: {}", bookInfo);
        try {
            Integer result = bookService.updateBookById(bookInfo);
            if (result > 0){
                return "";
            }
            return "内部错误,请联系管理员";
        }catch (Exception e){
            log.error("更新图书失败, e: ", e);
            return "内部错误,请联系管理员";
        }
    }

    //删除图书
    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        log.info("删除图书, bookInfo: {}", bookId);
        try {
            Integer result = bookService.deleteBookById(bookId);
            if (result > 0){
                return "";
            }
            return "内部错误,请联系管理员";
        }catch (Exception e){
            log.error("更新图书失败, e: ", e);
            return "内部错误,请联系管理员";
        }
    }

    //批量删除图书
    @RequestMapping("/batchDeleteBook")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids: {}", ids);
        Integer result = bookService.batchDeleteBookByIds(ids);
        if (result > 0){
            return "";
        }
        return "删除失败,请联系管理员";
    }
}
